package 设计模式.建造者模式;

/**
 * 自定义类-建造者模式
 *
 * @author luokexiong
 * @version 1.0 2021/2/7
 * @since 1.9.0
 */
public class NutritionFacts {

    private final int servingSize;
    private final int servings;
    private final int calories;
    private final int fat;
    private final int sodium;
    private final int carbohydrate;

    private NutritionFacts(int servingSize, int servings, int calories, int fat, int sodium, int carbohydrate) {
        this.servingSize = servingSize;
        this.servings = servings;
        this.calories = calories;
        this.fat = fat;
        this.sodium = sodium;
        this.carbohydrate = carbohydrate;
    }

    public static Builder builder() {
        return new Builder();
    }

    public static class Builder {
        // Optional parameters -initialized to default values
        private int servingSize;
        private int servings;
        private int calories = 0;
        private int fat = 0;
        private int sodium = 0;
        private int carbohydrate = 0;

        public Builder() {
        }

        public Builder calories(int val) {
            calories = val;
            return this;
        }

        public Builder fat(int val) {
            fat = val;
            return this;
        }

        public Builder sodium(int val) {
            sodium = val;
            return this;
        }

        public Builder carbohydrate(int val) {
            carbohydrate = val;
            return this;
        }

        public NutritionFacts build() {
            return new NutritionFacts(this.servingSize, this.servings, this.calories, this.fat, this.sodium, this.carbohydrate);
        }
    }

    @Override
    public String toString() {
        return "NutritionFacts{" +
            "servingSize=" + servingSize +
            ", servings=" + servings +
            ", calories=" + calories +
            ", fat=" + fat +
            ", sodium=" + sodium +
            ", carbohydrate=" + carbohydrate +
            '}';
    }

    public static void main(String[] args) {
        NutritionFacts facts = NutritionFacts.builder().calories(1).fat(1).build();
        System.out.println(facts);
        NutritionFacts facts1 = NutritionFacts.builder().carbohydrate(1).build();
        System.out.println(facts1);
    }
}
